package com.xmy.cultivate.controller;

import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.TranscriptSet;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/weiChar")
public class WeiChartController extends BaseController {

    @Autowired
    @Lazy
    IWeiChartService iWeiChartService;

    @Autowired
    @Lazy
    ITranscriptSetService iTranscriptSetService;

    @Autowired
    @Lazy
    ITranscriptService iTranscriptService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    RedisTemplate redisTemplate;

    @Autowired
    IWecharsBindService iWecharsBindService;

    @Autowired
    IMqPushService iMqPushService;

    @PostMapping("/writeTranscriptPush")
    public Result writeTranscriptPush(@RequestBody Map map) throws InterruptedException {
        String isStatistics = "0";//1已填写，0未填写

        if (map.get("isStatistics") != null){
            isStatistics = (String) map.get("isStatistics");
        }

        List<Integer> yearClassIdList = new ArrayList<>();

        if (map.get("yearClassIdList") != null){
            yearClassIdList = (List<Integer>) map.get("yearClassIdList");
        }

        TranscriptSet transcriptSet = iTranscriptSetService.getById("1646797065822404608");
        if (!transcriptSet.getWeicharPushStatus().equals(1)){
            return new Result(Code.ERROR, "", "推送关闭，请先打开允许推送");
        }

        if (transcriptSet.getWeicharPushDateTime() != null) {
            LocalDateTime localDateTime = LocalDateTime.now();
            Duration durationDiff = Duration.between(transcriptSet.getWeicharPushDateTime(),localDateTime);//between传入两个时间，当第二个时间大于第一个时，差值为正数，反之为负数
            long diffMinutes = durationDiff.toMinutes();
            if (diffMinutes < 60){
                return new Result(Code.ERROR, "", "距离上次推送"+diffMinutes+"分钟，超过60分钟后，才能推送下一次");
            }
        }

        Integer weicharPushCount = transcriptSet.getWeicharPushCount();
        weicharPushCount++;
        transcriptSet.setWeicharPushCount(weicharPushCount);
        transcriptSet.setWeicharPushDateTime(LocalDateTime.now());
        iTranscriptSetService.updateById(transcriptSet);

        Quarter quarter = iQuarterService.getQuarterForYearAndNum(transcriptSet.getPatriarchYearPart(), transcriptSet.getPatriarchQuarterNum());
        Integer yearPart = quarter.getYearPart();
        Integer quarterNum = quarter.getNum();
        Integer examNum = transcriptSet.getPatriarchExamNum();
        iTranscriptService.writeTranscriptPush(yearPart,quarterNum,examNum,isStatistics,quarter,yearClassIdList,weicharPushCount,transcriptSet);


        return new Result(Code.OK, "", "成功");
    }

    @GetMapping("/pushTest")
    public Result pushTest(@RequestParam("openId") String openId,
                           @RequestParam("studentId") String studentId,
                           @RequestParam("studentName") String studentName,
                           @RequestParam("subjectsName") String subjectsName,
                           @RequestParam("clientMsgId") String clientMsgId,
                           @RequestParam(value = "signType",defaultValue = "1",required = false) Integer signType
                           ){
        LocalDateTime localDateTime = LocalDateTime.now().withNano(0);
        /***************************************微信推送***************************************************/
        StringBuilder first = new StringBuilder();
        String keyword1 = studentName;
        StringBuilder keyword2 = new StringBuilder();
        keyword2.append(localDateTime.toString().replace("T", " "));
        StringBuilder keyword3 = new StringBuilder();

        String pushName = subjectsName;

        if (signType.equals(1)) {
            first.append(studentName + "已安全到校，请放心哦~");
            keyword3.append("【"+pushName+"】到校");
        } else {
            first.append(studentName + "," + subjectsName + "已下课");
            keyword3.append("【"+pushName+"】离校");
        }

        StringBuilder remark = new StringBuilder();
        remark.append("");
        iWecharsBindService.onOffSchoolMq(openId, first.toString(), keyword1, keyword2.toString(), keyword3.toString(), remark.toString(), localDateTime + "" + studentId);
        return new Result(Code.OK, "", "成功");
    }

    @GetMapping("/pushObj")
    public Result pushObj(@RequestParam("objStr") String objStr){
        iMqPushService.wecharPush(objStr);
        return new Result(Code.OK, "", "成功");
    }
}
